import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class Statistic extends JPanel{
	public averageVsEpisode statisticPanel[];
	public Statistic(){
		statisticPanel = new averageVsEpisode[3];
		setBackground(Color.BLACK);
		setLayout(new GridLayout(3,1));
		statisticPanel[0] = new averageVsEpisode();
		statisticPanel[1] = new averageVsEpisode();
		statisticPanel[2] = new averageVsEpisode();
		add(statisticPanel[0]);
		add(statisticPanel[1]);
		add(statisticPanel[2]);
		TitledBorder b = new TitledBorder("Average vs Episode");
		b.setTitleColor(Color.YELLOW);
		setBorder(b);
		
	}
	public void draw(Racer r){
		repaint();
		statisticPanel[r.componentNum-1].draw(r);
	}
	
	class averageVsEpisode extends JPanel{
		Racer r;
		public averageVsEpisode(){
			setPreferredSize(new Dimension(250,200));
		}
		public void paint(Graphics g){
			
			if(r!=null){
				int x[] = new int[r.xAverage.size()];
				int y[] = new int[r.yEpisodes.size()];
				if(r.componentNum==1)
					g.setColor(Color.RED);
				else if(r.componentNum==2)
					g.setColor(Color.PINK);
				else if(r.componentNum==3)
					g.setColor(Color.YELLOW);
				
				for(int i = 0;i<r.xAverage.size();i++){
					
					x[i] = getHeight() - r.xAverage.get(i);
					y[i] = r.yEpisodes.get(i)%getWidth();
					if(i%40==1)
						g.drawString("("+r.xAverage.get(i)+")"+r.componentNum, y[i],x[i]);
				
				}
				
				g.setColor(Color.GREEN);
				g.drawPolyline(y, x, r.xAverage.size());
			}
		}
		public void draw(Racer r){
			this.r = r;
			validate();
			repaint();
		}
	}
}
